import { playerModule } from './store/modules/player'
import Vue from 'vue'
import Router from 'vue-router'
import HomeView from './views/Home.vue'
import ViewUI from 'view-design'

Vue.use(Router)

//@ts-ignore
let loginGuard = (to, from, next) => {
  if (playerModule.authToken === '' || playerModule.player.id === '') {
    next({ name: 'login' })
  } else next()
}

const router = new Router({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () =>
        import(/* webpackChunkName: "login" */ './views/Login/common.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () =>
        import(/* webpackChunkName: "login" */ './views/Login/Register.vue'),
      props: true
    },
    {
      path: '/roomlist',
      name: 'room-list',
      component: () =>
        import(/* webpackChunkName: "room" */ './views/RoomList.vue'),
      alias: '/',
      beforeEnter: loginGuard
    },
    {
      path: '/room/:id',
      name: 'room',
      component: () =>
        import(/* webpackChunkName: "room" */ './views/Room.vue'),
      props: true,
      beforeEnter: loginGuard
    },
    {
      path: '/me/:prevPath',
      name: 'me',
      component: () =>
        import(/* webpackChunkName: "player" */ './views/Player.vue'),
      props: true,
      beforeEnter: loginGuard
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () =>
        import(/* webpackChunkName: "about" */ './views/About.vue'),
      beforeEnter: loginGuard
    },
    {
      path: '/test',
      name: 'test',
      component: () => import(/* webpackChunkName: "test" */ './views/Test.vue')
    },
    {
      path: '*',
      component: () =>
        import(/* webpackChunkName: "error" */ './views/Error.vue')
    }
  ]
})

router.beforeEach((to, from, next) => {
  //@ts-ignore
  ViewUI.LoadingBar.start()
  next()
})

router.afterEach(route => {
  //@ts-ignore
  ViewUI.LoadingBar.finish()
})
export default router
